 function getParam(){
            var url = location.search+'';
            var astr = url.substr(1);  //去掉'?'
            astr = decodeURIComponent(astr); //解码
            var atemp = new Array();
            atemp = astr.split('&');

            var amap = new Map();
            for(var i=0 ;i<atemp.length; i ++){
                var ai = atemp[i];
                if(ai.indexOf('=') >=0){
                    var atp = ai.split('=');
                    amap.set(atp[0],atp[1]);
                }       
            }
            return amap;
 }
function parseNum(val, scale) {
    if (isNaN(val) == true || val == undefined || val == '') {
        return 0;
    }
    // 不设的话默认不带小数
    if (scale == null || scale == undefined) {
        scale = 2;
    }
    val = parseFloatExtension(val);

    return parseFloatExtension(Math.round(val * Math.pow(10,scale)) / Math.pow(10,scale));
}

function parseFloatExtension(val){
    return Math.round(parseFloat(val) * Math.pow(10,12))/Math.pow(10,12);
}

function clearProductInfo(idx) {
    var tr1 = document.getElementById('repeat1_tr_' + idx);
    var tr2 = document.getElementById('repeat2_tr_' + idx);
    var left = tr1.getElementsByTagName('input');
    var right = tr2.getElementsByTagName('input');
    for (var i = 0; i < left.length; i++) {
        var obj = left[i];
        obj.value = '';
    }
    for (var i = 0; i < right.length; i++) {
        var obj = right[i];
        obj.value = '';
    }
    // 清空
    // 产品名、Id
    document.getElementById('page:form:repeat1:' + idx + ':productName').value = '';
    document.getElementById('page:form:repeat1:' + idx + ':productName_lkid').value = '';
    document.getElementById('page:form:repeat1:' + idx + ':productId').value = '';
    // 品牌
    document.getElementById('brand_' + idx).innerText = null;
    // 体积
    //document.getElementById('page:form:repeat1:' + idx + ':volume').value = '';
    // 测试数
    document.getElementById('page:form:repeat2:' + idx + ':testNumShow').value = '';
    document.getElementById('page:form:repeat2:' + idx + ':testNum').value = '';
    // 税率
    document.getElementById('page:form:repeat2:' + idx + ':tax').value = '';
    // 标准报价
    document.getElementById('page:form:repeat2:' + idx + ':unitPrice').value= '';
    // 经销商每包装成本
   //document.getElementById('page:form:repeat2:' + idx + ':dealerPackCost').value = '';
    // 集团成本
    // document.getElementById('page:form:repeat2:' + idx + ':maccuraPackCost').value = '';
}


function specialPrice(val) {
    if (val == 0 || val == null) return 0;
    var t = val / snum;
    var p;
    if (t > 0 && t < 60) {
        p = 1.63;
    }
    if (t >= 60 && t < 70) {
        p = 1.47;
    }
    if (t >= 70 && t < 80) {
        p = 1.36;
    }
    if (t >= 80 && t < 90) {
        p = 1.28;
    }
    if (t >= 90 && t < 100) {
        p = 1.21;
    }
    if (t >= 100 && t < 120) {
        p = 1.16;
    }
    if (t >= 120 && t < 150) {
        p = 1.08;
    }
    if (t >= 150 && t < 180) {
        p = 1.01;
    }
    if (t >= 180 && t < 200) {
        p = 0.95;
    }
    if (t >= 200) {
        p = 0.93;
    }
    return val * p;
}

function calculateSummary() {
    // 总标本量销售额合计
    var sna = 0;
    var pna = 0;
    var sea = 0;
    var pea = 0;
    // 总成本合计
    var cna = 0;
    var cea = 0;
    // 总不含税收入合计
    var rna = 0;
    var rea = 0;
    // 迈克总成本合计
    var mcna = 0;
    var mcea = 0;
    var isGroup = window.location.pathname.toLocaleLowerCase().indexOf('group') >0 

    // 每年合计
    for (var i = 1; i <= yearCnt; i++) {
        // 每年标本量销售额合计
        var sn = 0;
        var pn = 0;
        var se = 0;
        var pe = 0;
        // 每年成本合计
        var cn = 0;
        var ce = 0;
        // 每年不含税收入合计
        var rn = 0;
        var re = 0;
        // 迈克每年成本合计
        var mcn = 0;
        var mce = 0;

        //资金成本合计
        var mcost = 0 ; 
        var mcost_group = 0 ; 


        for (var j = 0; j < dataCnt; j++) {
            // 没有产品就不计算了
            if (document.getElementById('page:form:repeat1:' + j + ':productId').value == '') continue;

            var t = document.getElementById('page:form:repeat1:' + j + ':type').innerText.trim();
            // 每年标本量销售额
            var s;
            if (i == 1) {
                s = document.getElementById('page:form:repeat2:' + j + ':sampleNum' + i).value;
            } else {
                s = document.getElementById('sampleNum' + i + '_' + j).value;
            }
            var p = document.getElementById('samplePrice' + i + '_' + j).value;
            // 每年成本
            var c = document.getElementById('dealerCost' + i + '_' + j).value;

            //是不是迈克品牌
            var isSelfBrand = document.getElementById('page:form:repeat1:'+j+':isSelfBrand').value == 'true';
            
            ///子公司 迈克销售额 * 账期 / 12 * 0.05 + 非迈克销售额 * 账期 / 12 * 0.09
            ///集团 销售额 * 账期 / 12 * 0.06
            mcost += p * accPeriod / 12 * (isGroup ? 0.06 : (isSelfBrand ? 0.05: 0.09));

            // 每年不含税收入
            // var r = document.getElementById('revenueWot' + i + '_' + j).value;
            // // 迈克每年成本
            // var m = document.getElementById('maccuraCost' + i + '_' + j).value;
            
            if (t == '现有') {
                sn += parseFloatExtension(s);
                pn += parseFloatExtension(p);
                cn += parseFloatExtension(c);
                // rn += parseFloatExtension(r);
                // mcn += parseFloatExtension(m);

                sna += parseFloatExtension(s);
                pna += parseFloatExtension(p);
                cna += parseFloatExtension(c);
                // rna += parseFloatExtension(r);
                // mcna += parseFloatExtension(m);
            }
            se += parseFloatExtension(s);
            pe += parseFloatExtension(p);
            ce += parseFloatExtension(c);
            // re += parseFloatExtension(r);
            // mce += parseFloatExtension(m);

            sea += parseFloatExtension(s);
            pea += parseFloatExtension(p);
            cea += parseFloatExtension(c);
            // rea += parseFloatExtension(r);
            // mcea += parseFloatExtension(m);
        }
        //资金成本
        document.getElementById('moneycostNum' + i).value = parseFloatExtension(mcost);

        // 现有
        document.getElementById('sampleNumSumNow' + i).value = parseNum(sn);
        document.getElementById('salesNumSumNow' + i).value = parseNum(pn);
        document.getElementById('salesNumSumNow_real' + i).value = parseFloatExtension(pn);
        document.getElementById('dealerCostSumNow' + i).value = parseNum(cn);
        document.getElementById('dealerCostSumNow_real' + i).value = parseFloatExtension(cn);
        // document.getElementById('revenueWotSumNow' + i).value = parseNum(rn);
        // document.getElementById('maccuraCostSumNow' + i).value = parseNum(mcn);
        // 含预计
        document.getElementById('sampleNumSumEsti' + i).value = parseNum(se);
        document.getElementById('salesNumSumEsti' + i).value = parseNum(pe);
        document.getElementById('salesNumSumEsti_real' + i).value = parseFloatExtension(pe);
        document.getElementById('dealerCostSumEsti' + i).value = parseNum(ce);
        document.getElementById('dealerCostSumEsti_real' + i).value = parseFloatExtension(ce);
        // document.getElementById('revenueWotSumEsti' + i).value = parseNum(re);
        // document.getElementById('maccuraCostSumEsti' + i).value = parseNum(mce);
    }
    // 总合计
    // 现有
    document.getElementById('sampleNumNowAll').value = parseNum(sna);
    document.getElementById('salesNumNowAll').value = parseNum(pna);
    document.getElementById('dealerCostNowAll').value = parseNum(cna);
    // document.getElementById('revenueWotNowAll').value = parseNum(rna);
    // document.getElementById('maccuraCostNowAll').value = parseNum(mcna);
    // 含预计
    document.getElementById('sampleNumEstiAll').value = parseNum(sea);
    document.getElementById('salesNumEstiAll').value = parseNum(pea);
    document.getElementById('dealerCostEstiAll').value = parseNum(cea);
    // document.getElementById('revenueWotEstiAll').value = parseNum(rea);
    // document.getElementById('maccuraCostEstiAll').value = parseNum(mcea);
}

// TODO 要不要做成共通
function copyTestNum(idx) {
    document.getElementById('page:form:repeat2:' + idx + ':testNum').value = document.getElementById('page:form:repeat2:' + idx + ':testNumShow').value;
}


function calculateRow(idx){

     var i = parseInt(idx);
    // 没有产品就不计算了
    if (document.getElementById('page:form:repeat1:' + i + ':productId').value == '') return;

    // var v = document.getElementById('page:form:repeat1:' + i + ':volume').value;
    // 测试数
    var tn = document.getElementById('page:form:repeat2:' + i + ':testNum').value;
    // 标准报价
    var up = document.getElementById('page:form:repeat2:' + i + ':unitPrice').value;
    // 客户价格
    var sp = document.getElementById('page:form:repeat2:' + i + ':salesPrice').value;
    // 税率
    var t = document.getElementById('page:form:repeat2:' + i + ':tax').value;
    // 第一年标本量
    var sn1 = document.getElementById('page:form:repeat2:' + i + ':sampleNum1').value;
    // 经销商每包装成本
    var dpc = document.getElementById('page:form:repeat2:' + i + ':dealerPackCost').value;
   // console.log(sn1 + ' '+ dpc +' '+ t);
    // var mpc = document.getElementById('page:form:repeat2:' + i + ':maccuraPackCost').value;
    // 存货代码
    var sno = document.getElementById('page:form:repeat1:' + i + ':serailNo').value;
    var scode = (sno == scode1 || sno == scode2 || sno == scode3);
    
    // 客户折扣
    var d;
    if (up == 0 || up == null) {
        d = 0;
    } else {
        d = sp / up;
    }
    document.getElementById('discount_' + i).value = parseNum(d, 2);
    // 客户每测试价
    var tp;
    if (tn == 0 || tn == null) {
        tp = 0;
    } else {
        tp = sp / tn;
    }
   // console.log(sp +' / '+ tn + ' = ' +tp);

    document.getElementById('testPrice_' + i).value = parseNum(tp, 2);

     // 产品毛利率
    var rcr;
    if (sp == 0 || sp == null) {
        rcr = 0;
    } else {
        rcr = 1 - (dpc / sp);
    }
    document.getElementById('costRateD_' + i).value = (parseNum(rcr * 100)) + '%';
    // 经销商折扣
    var dd;
    if (up == 0 || up == null) {
        dd = 0;
    } else {
        dd = dpc / up;
    }
    document.getElementById('dealerDiscount_' + i).value = parseNum(dd, 2);
    // 每测试成本
    var tc;
    if (tn == 0 || tn == null) {
        tc = 0;
    } else {
        tc = dpc / tn;
    }
    // console.log('没测试成本 '+dpc +' / '+ tn + ' = ' +tc);
    document.getElementById('testCostD_' + i).value = parseNum(tc, 2);
    ///费用比例
    var sr =  document.getElementById("page:form:repeat1:"+i+":saleratio").value;

    var sum_sample  = 0 ; //年累计样本量
    var sum_sale  = 0 ; //年累计销售额
    var sum_cost  = 0 ; //年累计成本
    ///循环计算
    for(var j = 1 ; j <= yearCnt ; j ++ ){
        var sps = 0; //当前年销售额 
        var sn = 0 ; //当前年样本量 
        var dc = 0 ; //当年成本 

        if(j == 1){
             sn = sn1 ; 
        }else{
            sn  = calculateSample(sn1 ,sirs, j -1 ); 
            //样本量
            document.getElementById('sampleNum'+j+'_' + i + '_show').value = parseNum(sn);
            document.getElementById('sampleNum'+j+'_' + i).value = parseFloatExtension(sn);
             
        }
        //销售额
        sps = scode == true ? specialPrice(sn) : sn * tp;
        document.getElementById('samplePrice'+j+'_' + i + '_show').value = parseNum(sps);
        document.getElementById('samplePrice'+j+'_' + i).value = parseFloatExtension(sps);
        sum_sample += parseFloatExtension(sn) ; 
        sum_sale += sps ;
        //成本
        dc = scode == true ? specialCost(sps) : tc * sn;
        document.getElementById('dealerCost'+j+'_' + i).value = parseNum(dc,2);
        sum_cost += dc ; 
        // 隐藏值：试剂销售
        document.getElementById('st'+j+'_' + i).value = (t == 0 || t == '') ? 0 : sps / (1+t/100.0);
        // 隐藏值：试剂成本
        document.getElementById('ct'+j+'_' + i).value = (t == 0 || t == '') ? 0 : dc / (1+t/100.0);
        // 隐藏值：销售费用
        document.getElementById('sarp'+j+'_' + i).value = sps * sr / 100;
        
    }

    document.getElementById('sampleNumSum_' + i + '_show').value = parseNum(sum_sample);
    document.getElementById('sampleNumSum_' + i).value = parseFloatExtension(sum_sample);
    document.getElementById('samplePriceSum_' + i + '_show').value = parseNum(sum_sale);
    document.getElementById('samplePriceSum_' + i).value = parseFloatExtension(sum_sale);
    document.getElementById('dealerCostSum_' + i).value = parseNum(sum_cost,2);

}
function calculateSample(initValue , ritos , times  ){

    for(var i = 0 ; i < times ; i ++){
        initValue *= (1+ ritos[i+1]/100);
    }
    return initValue;
}

///设置试剂销售费用比例
function setSaleRatio(){
    for (var j = 0; j < dataCnt; j++) {
        document.getElementById("page:form:repeat1:"+j+":saleratio_show").innerText = sar + '%';
        document.getElementById("page:form:repeat1:"+j+":saleratio").value = sar;

    }
}
/******************************************************/
/**
计算仪器的合计
*/
function sumInstrument() {
    var sic = 0;//仪器成本合计
    var sir = 0;//保修成本合计
    var sn = 0; //数量合计
    var sics = 0; //总成本合计
    var sirs = 0 ; //总维修成本合计
    if (insCnt > 0) {
        for (var i = 0; i < insCnt; i++) {
            ///仪器成本 :
            //console.log(window.location.href);
            var ic =  document.getElementById('page:form:InstrumentInfo:' + i + ':InstrumentCost').value;
            //保修成本 
            var ir = document.getElementById('page:form:InstrumentInfo:' + i + ':instrumentRepairCost').value;

            ///数量
            var n = document.getElementById('instrumentQuantity_show_' + i).value;

            ///总成本
            var ics = parseNum(ic,4)  * parseNum(n)  ;
            ///总修成本
            var irs = parseNum(ir,4)  * parseNum(n) ;
            //总成本
            document.getElementById('instrumentCostSum_' + i).value = ics;
            //总维修成本
            document.getElementById('instrumentRepairCostSum_'+ i).value = irs;

            
           // console.log(ic);
            sic += parseNum(ic,4) ;
            sir += parseNum(ir,4)  ;
            sn += parseNum(n)  ;
            sics += ics;
            sirs += irs;
            //总成本
            document.getElementById('instrumentCostSum_' + i).value = ic * n ;
            //总维修成本
            document.getElementById('instrumentRepairCostSum_'+ i).value = ir * n;


        }

        ///合计
       ///仪器成本合计
       document.getElementById('instrumentCost_show').value = sic;
        ///保修成本合计
       document.getElementById('instrumentRepairCost_show').value = sir;
        ///数量合计
       document.getElementById('instrumentQuantity_show').value = sn;
        ///总成本合计
       document.getElementById('instrumentCostSum_show').value = sics;
        ///总维修成本合计
       document.getElementById('instrumentRepairCostSum_show').value = sirs;

       
        var pn = window.location.pathname;
        if (pn.toUpperCase().indexOf('DEALER')< 0 ){
           ///设置仪器成本 
           document.getElementById('dppnShow').value = sic /10000.0;

            adjustInstCost(assize, sic /10000.0);

            calculateDealer();
        }


    }
}

function getRowsAndCols(w){
    var range = XLSX.utils.decode_range(w['!ref']);
    return range.e;
}

function concat_worksheet(w1,w2,origin){

    var merges = w2['!merges']; ///console.log(merges);
    var new_merges = w1['!merges'];
    var newo = XLSX.utils.decode_cell(origin);
    var range = XLSX.utils.decode_range(w2['!ref']);
    var old_range = XLSX.utils.decode_range(w1['!ref']);

    var new_ref = {s:{r:0,c:0},e:{r:old_range.e.r,c:old_range.e.c}};
    for(var R = range.s.r; R <= range.e.r; ++R) {
      for(var C = range.s.c; C <= range.e.c; ++C) {
         var cell_address = {c:C , r:R};
        var cell_ref = XLSX.utils.encode_cell(cell_address);
        var new_cell_address = {c:(C+newo.c) , r:(R+newo.r)};
        new_ref.e.r = new_ref.e.r > R+newo.r ? new_ref.e.r :  R+newo.r;
        new_ref.e.c = new_ref.e.c > C+newo.c ? new_ref.e.c :  C+newo.c;
        var new_cell_ref = XLSX.utils.encode_cell(new_cell_address);
        w1[new_cell_ref] = w2[cell_ref];
        
      }
    }
    for(var i = 0 ; i< merges.length ; i ++){
        var m = merges[i];
        m.s.c += newo.c ;
        m.s.r += newo.r ;
        m.e.c += newo.c ;
        m.e.r += newo.r ;
        new_merges.push(m);
    }
    w1['!merges'] = new_merges;
    w1['!ref'] = XLSX.utils.encode_range(new_ref);
    return w1;
}


            

function getExcelRes(){
    var ws1 =XLSX.utils.table_to_sheet(document.getElementsByClassName('t')[0]);
    var ws2 =XLSX.utils.table_to_sheet(document.getElementsByClassName('t')[1]);

    if(Object.values == undefined){

                Object.values = function (obj){
                    var tmp = [];
                    var keys = Object.keys(obj);
                    for(i=0;i < keys.length; i ++){
                        tmp.push(obj[keys[i]]);
                    }
                    return tmp;
                }
            }
            
    var ds = Object.values(ws2);
    for(var i in ds ){
         if(ds[i].t != undefined){
              ds[i].t = "s";
         }
    }
    var ws3 =XLSX.utils.table_to_sheet(document.getElementsByClassName('t')[2]);
    var ws4 =XLSX.utils.table_to_sheet(document.getElementsByClassName('t')[3]);

    
    var wb  = XLSX.utils.book_new();
    var or1 = XLSX.utils.encode_cell({r:0,c:getRowsAndCols(ws1).c+1});
    var or2 = XLSX.utils.encode_cell({r:getRowsAndCols(ws1).r+1,c:0});
    var or3 = XLSX.utils.encode_cell({r:getRowsAndCols(ws1).r+1,c:getRowsAndCols(ws1).c+1});

    var neww = concat_worksheet(ws1,ws3,or1);
    neww =  concat_worksheet(neww,ws2,or2);
    neww =  concat_worksheet(neww,ws4,or3);
    var or4 = XLSX.utils.encode_cell({r:getRowsAndCols(neww).r+2,c:0});

    try{

        var ws5 =XLSX.utils.table_to_sheet(document.getElementsByClassName('t')[4]);

        var ws6 =XLSX.utils.table_to_sheet(document.getElementsByClassName('t')[5]);
        var ws7 =XLSX.utils.table_to_sheet(document.getElementsByClassName('t')[6]);
        var ws8 =XLSX.utils.table_to_sheet(document.getElementsByClassName('t')[7]);
        //var ws9=XLSX.utils.table_to_sheet(document.getElementsByClassName('t')[8]);
        //var ws10 =XLSX.utils.table_to_sheet(document.getElementsByClassName('t')[9]);
        //var ws11 =XLSX.utils.table_to_sheet(document.getElementsByClassName('t')[10]);
        neww =  concat_worksheet(neww,ws5,or4);
        neww =  concat_worksheet(neww,ws6,XLSX.utils.encode_cell({r:getRowsAndCols(neww).r+2,c:0}));
        neww =  concat_worksheet(neww,ws7,XLSX.utils.encode_cell({r:getRowsAndCols(neww).r+2,c:0}));
        //neww =  concat_worksheet(neww,ws8,XLSX.utils.encode_cell({r:getRowsAndCols(neww).r+2,c:0}));
        //neww =  concat_worksheet(neww,ws9,XLSX.utils.encode_cell({r:getRowsAndCols(neww).r+2,c:0}));
        //neww =  concat_worksheet(neww,ws10,XLSX.utils.encode_cell({r:getRowsAndCols(neww).r+2,c:0}));
        //neww =  concat_worksheet(neww,ws11,XLSX.utils.encode_cell({r:getRowsAndCols(neww).r+2,c:0}));
    }catch(e){

    }
    

    XLSX.utils.book_append_sheet(wb, neww, 'sheet1');

    //设置表格的样式
    var wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: false, type: 'binary', showGridLines: true }); 
    var s2ab=function(s) {
    let buf = new ArrayBuffer(s.length);
    let view = new Uint8Array(buf);
    for (let i = 0; i !== s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
        return buf;
    };
    saveAs(new Blob([s2ab(wbout)], { type: 'application/octet-stream' }), '测算结果.xlsx')
}












